package Top200;

import java.util.LinkedList;
import java.util.List;

/**
 * @author zhangmin
 * @create 2022-02-22 10:57
 * 回溯
 */
public class permute46 {
    List<List<Integer>> res=new LinkedList<>();
    boolean[] visited;
    void backtrack(int[] nums,LinkedList<Integer> path){
        if (path.size()==nums.length){
            res.add(new LinkedList<>(path));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (visited[i]){
                continue;
            }
            path.addLast(nums[i]);
            visited[i]=true;
            backtrack(nums,path);
            visited[i]=false;
            path.pollLast();
        }
    }
    public List<List<Integer>> permute(int[] nums) {
        visited=new boolean[nums.length];
        backtrack(nums,new LinkedList<>());
        return res;
    }
}
